package com.epalmpay.service.activity.impl;

import javax.annotation.Resource;

import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.activity.ActivityBaseInDTO;
import com.epalmpay.dto.activity.ActivityBaseListOutDTO;
import com.epalmpay.dto.activity.ActivityBaseOutDTO;
import com.epalmpay.entity.*;
import com.epalmpay.enumdef.BizEnum;
import com.epalmpay.enumdef.CommonEnum;
import com.epalmpay.mapper.*;
import com.epalmpay.service.groupaccount.IGroupAccountService;
import com.epalmpay.service.weixin.IWalletService;
import com.epalmpay.util.StringUtil;
import com.epalmpay.util.payplat.UtilFun;
import org.springframework.stereotype.Service;

import com.epalmpay.service.activity.IActivityBaseService;

import java.util.*;

/**
 * Created by Administrator on 2017/5/26.
 */
@Service("activityBaseService")
public class ActivityBaseServiceImpl implements IActivityBaseService{
	@Resource
	private IWalletService walletService;
    @Resource
    private ActivityBaseMapper activityBaseMapper;
    @Resource
    private TradeOfflineMapper tradeOfflineMapper;
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private GroupMapper groupMapper;
    @Resource
    private GroupWxconfigMapper groupWxconfigMapper;
    @Resource
    private IGroupAccountService groupAccountService;

    @Resource
    private MallEntityGoodMapper mallEntityGoodMapper;

    @Override
    public int UpdateActivityBaseByActivityId(ActivityBase activityRoutine) {

        int status = activityBaseMapper.updateByPrimaryKeyActivityId(activityRoutine);
        return status;
    }

    @Override
    public int insertSendCashOnline(Member member, Integer amount, Long activityId) throws Exception{

        String OrgOrderNum = UtilFun.createOrderNum();
        //调用充值接口

        //调用支付平台WeiXin充值接口
        Group group = groupMapper.selectByPrimaryKey(member.getGroupId());

       

        //插入线上交易表
        TradeOffline record = new TradeOffline();
        record.setGroupId(member.getGroupId());
        record.setOrderId(OrgOrderNum);
        record.setPayOrderid(OrgOrderNum);
        record.setPayerId(member.getGroupId());
        record.setPayerName(group.getMerchantName());
        record.setPayerType((byte) CommonEnum.UserType.BusinessManager.getType());
        record.setReceiverId(member.getId().toString());
        record.setReceiverName(member.getName());
        record.setReceiverType((byte)CommonEnum.UserType.User.getType());
        record.setOrderAmount(amount);
        record.setRealAmount(amount);
        record.setTransactionType((byte)BizEnum.TransactionType.ConsumptionBackCash.getType());
        record.setPayType((byte)BizEnum.PaymentType.Wallet.getType());
        record.setSubType((byte)BizEnum.PaymentScene.Online.getType());
        record.setInNetCompany((byte)BizEnum.InNetCompany.fuqianla.getType());
        record.setTransactionStatus((byte)BizEnum.TransactionStatus.Success.getType());
        record.setIsWallet(Boolean.TRUE);
        record.setGmtCreate(new Date());
        record.setGmtModified(new Date());
        //record.setPaySelfOrderid(OrgOrderNum);
   //   record.setTransOrderNo(payResult.getOrderId());
        record.setTransOrderNo(null);
        record.setDetail("线上消费送");
        tradeOfflineMapper.insertSelective(record);

        //更新预算流水
        groupAccountService.updateAmount(member.getGroupId(), BizEnum.SaleAccountSubjectType.ConsumeBackCashGet,
                amount,record.getId(),activityId,"线上消费送");
        
        walletService.updateUserLockedAmount(member.getVirtualCard(), member.getPayGroupCode(), member.getUserId(), amount+"", "0", 0,OrgOrderNum);
        return 1;
    }

    @Override
    public ActivityBaseListOutDTO queryActivityList(ActivityBaseInDTO activityBaseInDTO, Page page) throws Exception {

        ActivityBaseListOutDTO activityBaseListOutDTO = new ActivityBaseListOutDTO();

        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId", activityBaseInDTO.getGroupId());
        param.put("status", activityBaseInDTO.getStatus());
        param.put("joinStart", activityBaseInDTO.getJoinStart());
        param.put("joinEnd",activityBaseInDTO.getJoinEnd());
        param.put("shopId", activityBaseInDTO.getShopId());
        param.put("activityName", activityBaseInDTO.getActivityName());


        int sumCount = activityBaseMapper.queryShopActivityCount(param);
        page.setTotalResult(sumCount);
        if(sumCount<=0)
            return activityBaseListOutDTO;

        param.put("limit", page.getShowCount());
        param.put("start", page.getShowCount()*(page.getCurrentPage()-1));


        List<ActivityBaseOutDTO> activityBaseOutDTOList = activityBaseMapper.queryShopActivityList(param);
        activityBaseListOutDTO.setActivityBaseOutDTOList(activityBaseOutDTOList);
        return activityBaseListOutDTO;
    }
}
